package org.agilelang.mvc;

/*
 AgileLang - A fourth generation language(http://www.agilelang.org).

 Copyright (C) 2013 Roderick Cusirramos (roderick20@hotmail.com)

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.io.*;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JTextArea;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import org.agilelang.object.Application;
import org.agilelang.object.Project;


public class Facade {

    protected String path_source = "";
    protected String path_xml = "";
    protected String path_app = "";
    protected Project project = null;
    public boolean error=false;

    public void process() {
        path_xml = (new File(path_source)).getParentFile().getAbsolutePath() + "/meta.xml";
        scanner_parser(new JTextArea());
        readProject();
    }

    public void scanner_parser(JTextArea text) {
        /*Scanner scaner = new Scanner();
        text.append("Scaner : " + path_source + "\n");

        List<File> files = addFiles(null, new File(path_source));
        System.out.println(files);
        for (int i = 0; i < files.size(); i++) {
            File lenguaje = files.get(i);
            text.append("file " + lenguaje.getAbsolutePath() + "\n");
            if (!scaner.process(lenguaje.getAbsolutePath())) {
                //System.err.println(scaner.getError());
                text.append("ERROR : " + scaner.getError() + "\n");
                return;
            }
        }


        text.append("Parser..." + "\n");

        Parser parser = new Parser();
        parser.process(scaner.getLines());
        try {
            path_xml = (new File(path_source)).getParentFile().getAbsolutePath() + "/meta.xml";
            PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(path_xml)));
            System.out.println(path_xml);
            pw.print(parser.getXml());
            pw.close();
            text.append("XML Generate : " + path_xml + "\n");
        } catch (IOException e) {
            text.append("ERROR : " + e.toString() + "\n");
        }*/
    }

    public Project readProject() {
        File file = new File(path_xml);
        if (!file.exists()) {
            System.err.println("Fichero XML no existe");
            return null;
        }
        //leer xml
        InputStream is = null;
        Reader reader = null;
        try {
            is = new FileInputStream(path_xml);
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            System.out.println("Reding xml...");
        } catch (Exception ex) {
            System.err.println("Error al leer fichero XML : " + ex);
        }
        //Poblar objecto Project        
        JAXBContext jc;
        try {
            jc = JAXBContext.newInstance(Project.class);
            Unmarshaller u = jc.createUnmarshaller();
            project = (Project) u.unmarshal(reader);
            System.out.println("Built project...");
        } catch (JAXBException ex) {
            System.err.println("Error al poblar objeto Project : " + ex.toString());
        }
        return project;
    }

    public Application getApp() {
        return project.application;
    }

    public Project getProject() {
        return project;
    }

    public List<File> addFiles(List<File> files, File dir) {
        if (files == null) {
            files = new LinkedList<File>();
        }

        if (!dir.isDirectory()) {
            files.add(dir);
            return files;
        }

        for (File file : dir.listFiles()) {
            if (file.getName().endsWith(".agile")) {
                addFiles(files, file);
            }
        }
        return files;
    }
}
